<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Diets extends MY_Controller {

	function __construct(){
		parent::__construct();
		//饮食
		$this->load->model('User/UserDietsModel', 'item');
	}	
	
	
	//列表
	public function get_list(){		
		$user_id = $this->user_id;
		$page	= $this->input->post('page');
		$size	= $this->input->post('size');
		$type	= $this->input->post('type',null,0);
		
		
		if($page<1)$page=1;
		if($size<1)$size=10;

		$arr = array(
			"status" => 0,
			"type"   => $type
		);
		
		$res = $this->item->get_list($arr,$page,$size);
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
	
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
	//详细
	public function get_info(){		
		$id		= $this->input->post('id',null,'0');		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$res = $this->item->get_info($id);
		
		$data['result_code'] = 0;
		$data['message']     = '成功';
		$data['data'] 		 = $res;
	
		$this->out($data);
	}	
	//全部
	public function get_lists(){
		$res = $this->item->get_lists();
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	//添加饮食打卡
	public function add_diets(){
		$user_id = $this->user_id;
		$type  = $this->input->post('type');
		$title = $this->input->post('title');
		$info  = $this->input->post('info');
		$theme_img = $this->input->post('theme_img',null,'');//主题图
		$status  = $this->input->post('status',null,0);		//状态 0公开 1仅自己可看
		$position = $this->input->post('position');
		
		$time = time();
		
		if(!$title){
			$this->err_out('请输入标题');
		}
		if(!$info){
			$this->err_out('请填写内容');
		}
		if(!$theme_img){
			$this->err_out('请上传图片');
		}
		
		$arr = array(
			"user_id" 	=> $user_id,
			"type" 		=> $type,
			"title" 	=> $title,
			"info" 		=> $info,
			"date" 		=> date("Y-m-d",$time),
			"addtime" 	=> $time,
			"theme_img" => $theme_img,
			"status"	=> $status,
			"position"  => $position
		);
		
		$res = $this->item->add($arr);
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'发表成功':'发表失败';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	
	
	
}
